<template>
	<view class="p-10">
		<view class="card">
			<my-forms ref="valiForm" :model="valiFormData" :label-position="'top'" :formItem="formData">
			</my-forms>
			<view class="flex" v-if="currentKey == 0 && valiFormData.showRecieveAndRejectSign == 0">
				<!-- <u-button style="width: 200rpx" type="primary" shape="circle" text="风险管控" @click="click"></u-button> -->
				<u-button class="m-t-10" style="width: 200rpx" type="primary" shape="circle" text="隐患录入"
					@click="click1"></u-button>
				<!-- <u-button class="m-t-10" style="width: 200rpx" type="primary" shape="circle" text="结束任务" @click="click2"></u-button> -->
			</view>
			<view class="flex" v-if="currentKey == 0 && valiFormData.showRecieveAndRejectSign == 1">
				<u-button style="width: 200rpx" type="primary" shape="circle" text="领取任务" @click="click3"></u-button>
				<u-button class="m-t-10" style="width: 200rpx" type="primary" shape="circle" text="拒绝任务"
					@click="click4"></u-button>
			</view>
		</view>
	</view>
</template>
<script setup>
	import {
		onLoad,
	} from '@dcloudio/uni-app'
	import {
		ref,
		unref,
		computed,
		reactive
	} from 'vue'
	import {
		saveAndOut,
		getRecieve,
		getReject
	} from '@/api/riskcontrol.js'

	let formData = computed(() => {
		return [{
			label: '管控类型',
			prop: 'riskControlTypeName',
			disabled: true
		}, {
			label: '管控时间',
			prop: 'riskControlDate',
			disabled: true
		}, {
			label: '管控人',
			prop: 'users',
			disabled: true
		}, {
			label: '排查名称',
			prop: 'riskControlName',
			show: valiFormData.riskControlType == 1,
			disabled: true
		}, {
			label: '管控地点',
			prop: 'riskLocationName',
			disabled: true
		}, {
			label: '风险等级',
			prop: 'riskLevelNames',
			disabled: true
		}, ].filter(i => i)
	})
	const valiForm = ref(null)
	let valiFormData = reactive({})
	let currentKey = ref(0)
	onLoad((e) => {
		valiFormData = JSON.parse(e.data)
		valiFormData.users = JSON.parse(e.data).users.map(i => i.userName).join(',')
		currentKey.value = e.key
	})
	// 整改录入
	const click = () => {
		uni.navigateTo({
			url: '/pages/professional/hiddenTrouble/rectify/enter?id=' + unref(options).id + '&key=1'
		})
	}
	// 隐患录入
	const click1 = () => {
		uni.navigateTo({
			url: '/pages/professional/main/hiddenEntry/index'
		})
	}
	// 结束任务
	const click2 = () => {
		uni.showModal({
			content: '结束任务后，不能再进行操作了，确认结束吗?',
			success: async function(res) {
				if (res.confirm) {
					const {
						code
					} = await saveAndOut({
						controlInfoId: options.value.id
					})
					if (code == 200) {
						uni.showToast({
							title: "已结束任务",
							icon: "success",
							success() {
								uni.navigateBack({
									delta: 1
								})
							}
						});
					}
				}
			}
		})
	}
	// 领取任务
	const click3 = () => {
		uni.showModal({
			content: '确认领取任务吗?',
			success: async function(res) {
				if (res.confirm) {
					const {
						code
					} = await getRecieve({
						id: options.value.id
					})
					if (code == 200) {
						uni.showToast({
							title: "已领取任务",
							icon: "success",
							success() {
								uni.navigateBack({
									delta: 1
								})
							}
						});
					}
				}
			}
		})
	}
	// 拒绝任务
	const click4 = () => {
		uni.showModal({
			content: '确认拒绝任务吗?',
			success: async function(res) {
				if (res.confirm) {
					const {
						code
					} = await getReject({
						id: options.value.id
					})
					if (code == 200) {
						uni.showToast({
							title: "已拒绝任务",
							icon: "success",
							success() {
								uni.navigateBack({
									delta: 1
								})
							}
						});
					}
				}
			}
		})
	}
</script>
<style>
</style>